#include <Windows.h>
#include <strsafe.h>
#include <gdiplus.h>

#include "CTinyList.hpp"

LRESULT OnPaint(HDC hdc)
{
    // Gdiplus::Graphics graphics(hdc);
    // graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
    // int iWidth = m_Rect.right - m_Rect.left;
    // int iHeight = m_Rect.bottom - m_Rect.top;
    // Gdiplus::Bitmap membmp(iWidth, iHeight);
    // Gdiplus::Graphics *pGraphics = Gdiplus::Graphics::FromImage(&membmp);

    // Gdiplus::SolidBrush fillBursh(Gdiplus::Color(GetRValue(m_BkColor), GetRValue(m_BkColor), GetRValue(m_BkColor)));
    // pGraphics->FillRectangle(&fillBursh, Gdiplus::Rect(m_Rect.left, m_Rect.top, m_Rect.right, m_Rect.bottom));

    // delete pGraphics;
    // pGraphics = NULL;
    // return LRESULT(0);
}
CTinyList g_List;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    switch (uMsg)
    {
    case WM_CREATE:
    {
        RECT rcClient;
        // rcClient = {10, 10, 600, 800};
        GetClientRect(hWnd, &rcClient);
        g_List.SetHostWnd(hWnd);
        g_List.SetBound(rcClient);
        g_List.SetFontSize(12);
        g_List.SetTxtColor(RGB(0, 0, 0));
        g_List.SetItemHeight(32, 50);
        g_List.SetFamily(TEXT("Consolas"));
        g_List.SetBackColor(RGB(225, 225, 225));
        //选中背景色
        g_List.SetSelItemBColor(RGB(0, 225, 0));
        g_List.Init(32, RGB(0, 0, 0), RGB(180, 180, 180));
        g_List.InsertColumn(0, TEXT("hao"), 100);
        g_List.InsertColumn(1, TEXT("gender"), 100);
        g_List.InsertColumn(2, TEXT("identity"), 300);
        g_List.InsertColumn(3, TEXT("address"), 100);

        for (int i = 0; i < 30; i++)
        {
            TCHAR szValue[64] = {0};
            StringCchPrintf(szValue, 64, TEXT("Test: %d"), i + 1);
            g_List.InsertItem(i, szValue);

            g_List.SetItemText(i, 1, TEXT("65535"));
            g_List.SetItemText(i, 2, TEXT("114514"));
            g_List.SetItemText(i, 3, TEXT("China Zhengzhou"));
        }
    }
    break;
    case WM_PAINT:
    {
        hdc = BeginPaint(hWnd, &ps);
        g_List.DoDraw(hdc);
        EndPaint(hWnd, &ps);
        // MessageBox(hWnd, TEXT("WM_PAINT"), TEXT("WM_PAINT"), MB_OK);
    }
    break;
    case WM_SIZE:
    {
    }
    break;
    case WM_COMMAND:
    {
    }
    break;
    case WM_LBUTTONDOWN:
    {
        POINT pt;
        pt.x = LOWORD(lParam);
        pt.y = HIWORD(lParam);
        g_List.OnLButtonDown(hWnd, pt);
    }
    break;
    case WM_LBUTTONUP:
    {
        POINT pt;
        pt.x = LOWORD(lParam);
        pt.y = HIWORD(lParam);
        g_List.OnLButtonUp(hWnd, pt);
    }
    break;
    case WM_ERASEBKGND:
    {
        return FALSE;
    }
    break;
    case WM_MOUSEMOVE:
    {
        POINT pt;
        pt.x = LOWORD(lParam);
        pt.y = HIWORD(lParam);
        g_List.OnMouseMove(hWnd, pt);
    }
    break;
    case WM_KEYDOWN:
    {
    }
    break;
    case WM_CLOSE:
    {
        DestroyWindow(hWnd);
    }
    break;
    case WM_DESTROY:
    {
        PostQuitMessage(0);
    }
    break;
    default:
    {
    }
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

// WINAPI代表__stdcall 参数的传递顺序 从右到左 依次入栈 并且在函数返回前 依次出栈
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc;
    // Initialize GDI+.
    Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    wc.cbClsExtra = 0;                                     // 类的额外内存
    wc.cbWndExtra = 0;                                     // 窗口额外内存
    wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); // 背景
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);              // 光标 第一个为NULL代表使用系统提供的光标
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);            // 图标 第一个为NULL代表使用系统提供的图标
    wc.hInstance = hInstance;                              // 应用程序实例句柄
    wc.lpfnWndProc = WindowProc;                           // 回调函数
    wc.lpszClassName = TEXT("main");                       // 窗口类名称
    wc.lpszMenuName = NULL;                                // 菜单名称
    wc.style = CS_HREDRAW | CS_VREDRAW;                    // 显示风格

    RegisterClass(&wc);

    /*****
     * CreateWindow()
     * lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam
     * 类名
     * 标题名
     * WS_OVERLAPPEDWINDOW 混合风格
     * x坐标 CW_USEDEFAULT
     * y坐标
     * 宽
     * 高
     * 父窗口 NULL
     * 菜单 NULL
     * 实例句柄 hInstance
     * 附加值
     */
    HWND hWnd = CreateWindow(wc.lpszClassName, TEXT("Windows"), WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             NULL, NULL, hInstance, NULL);
    if (hWnd == NULL)
    {
        return 0;
    }
    ShowWindow(hWnd, SW_SHOWNORMAL);
    UpdateWindow(hWnd);

    /*****
     * HWND hwnd; 主窗口句柄
     * UINT message; 具体消息名称
     * WPARAM wParam; 附加消息 键盘消息
     * LPARAM lParam; 附加消息 鼠标消息
     * DWORD time; 消息产生时间
     * POINT pt; 附加消息 鼠标消息 x y
     */
    MSG msg;

    /*****
     * LPMSG lpMsg,HWND hWnd,UINT wMsgFilterMin,UINT wMsgFilterMax
     * 消息
     * 捕获窗口 NULL代表捕获所有窗口
     * 最小的过滤消息 0代表捕获所有消息
     * 最大的过滤消息 0代表捕获所有消息
     */
    while (GetMessage(&msg, NULL, 0, 0))
    {
        // 如果按的是组合键 需要翻译
        TranslateMessage(&msg);
        // 单按一个键盘就直接分发消息
        DispatchMessage(&msg);
    }
    Gdiplus::GdiplusShutdown(gdiplusToken);
    return 0;
}